<?php
/**
 * @ClassName: Dicttype
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-06 17:07
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Dicttype extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\DictType();
        $this->pageTitle = '字典类型';
        $this->enableField = 'status';
        $this->pagesize = 10;

        $this->selectSearch = 'name|flag';
        $this->selectFields = 'id,name,flag'; //优化查询
        $this->selectTextField = '{id}#{name}';
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;

        $search->text('name', '字典名称');
        $search->text('flag', '字典标识');
        $search->select('status', '状态')->options([1 => '启用', 0 => '停用']);
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['name'])) {
            $where[] = ['name', 'like', '%' . $searchData['name'] . '%'];
        }
        if (!empty($searchData['flag'])) {
            $where[] = ['flag', 'like', '%' . $searchData['flag'] . '%'];
        }
        if (isset($searchData['status']) && $searchData['status'] != '') {
            $where[] = ['status', 'eq', $searchData['status']];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');

        $table->show('name', '字典名称');
        $table->show('flag', '字典标识');
        $table->switchBtn('status', '状态')->autoPost('', true);
        $table->show('remark', '备注');

        $table->show('create_time', '添加时间');
        $table->show('update_time', '修改时间');

        $table->sortable('id, name, flag, create_time, update_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnLink('dict_data', url('/admin/dictdata/index', ['dict_type' => '__data.pk__']), '字典数据', 'btn-info', 'mdi mdi-coin', 'title="字典数据管理" data-layer-size="98%,98%"');
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->text('name', '字典名称')->required();
        $form->text('flag', '字典标识')->required();
        $form->radio('status', '状态')->options([1 => '启用', 0 => '停用'])->default(1)->required();
        $form->text('remark', '备注');

        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '修改时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'name',
            'flag',
            'status',
            'remark'
        ], 'post');

        $result = $this->validate($data, [
            'name|名称' => 'require',
            'flag|标识' => 'require',
        ]);

        if (true !== $result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();

            $res = $this->dataModel->where('flag', '=', $data['flag'])->where('id', '<>', $id)->find();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();

            $res = $this->dataModel->where('flag', $data['flag'])->find();
        }

        if ($res) {
            $this->error('字典标识已经存在');
        }

        return $this->doSave($data, $id);
    }

}